#!/usr/bin/with-contenv bashio
# shellcheck shell=bash
# vim: ft=bash
# ==============================================================================
# Take down the S6 supervision tree based on service exit code
# ==============================================================================

/usr/local/bin/srat-cli \
  -db /config/config.db3 \
  -loglevel $(bashio::config 'log_level' 'info') \
  stop \
  2>&1

if [[ "${1}" -ne 0 ]] && [[ "${1}" -ne 256 ]]; then
  bashio::log.warning "srat crashed, halting add-on"
  exec /run/s6/basedir/bin/halt
fi

bashio::log.info "srat stopped"
